package com.pansoft.openplanet.activity;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.AssetListBean;
import com.pansoft.openplanet.db.AccountDao;
import com.pansoft.openplanet.util.EthTokenTransactionManager;

import net.sf.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
@Deprecated
public class NewTransferSuccessActivity extends TCBaseActivity {

    private static final String TAG = "NewTransferSuccessActivity";

    private TextView tvCode, tvCodeIntro, tvMyAddressIntro, tvMyAddress,
            tvToAddressIntro, tvToAddress, tvCount, tvCountUnit, tvTransStatus, tvTitle;
    private ImageView ivTranferStatus;
    private AnimationDrawable animationDrawable;


    String amount;//金额
    String note; //备注
    String receiveAddress;//收款地址
    String payAddress; //付款地址
    String gas;//转账费用
    AssetListBean assetListBean;
    private EthTokenTransactionManager transactionManager;
    private AccountDao accountDao;

    private String transferTime;
    private TextView tvTime;
    private TextView tvTimeValue;
    private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMddHHmmss");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.talk_chain_activity_transfer_success);
        accountDao = new AccountDao(this);
        amount = getIntent().getStringExtra("amount");
        receiveAddress = getIntent().getStringExtra("receiveAddress");
        payAddress = getIntent().getStringExtra("payAddress");
        gas = getIntent().getStringExtra("gas");
        assetListBean = (AssetListBean) getIntent().getSerializableExtra("assetBean");
        initView();
       // initData();
    }

//    private void initData() {
//        transactionManager = new EthTokenTransactionManager(this);
//        Account account = accountDao.getAccount(payAddress);
//        if (account != null)
//            transactionManager.loadCredentialsASync(account,
//                    new EthTokenTransactionManager.LoadCredentialsCallBack() {
//                        @Override
//                        public void loadResult(boolean isSuccess, final Credentials credentials) {
//                            if (!isSuccess) {
//                                onFailAnim();
//                                return;
//                            }
//                            //2.获取nonce
//                            LogUtils.i("获取nonce", credentials.getAddress());
//                            transactionManager.getTransactionNonce(payAddress, new EthTokenTransactionManager.NonceCallBack() {
//                                @Override
//                                public void nonceResult(boolean isSuccess, BigInteger nonce) {
//                                    if (!isSuccess) {
//                                        onFailAnim();
//                                        return;
//                                    }
//                                    //3.开始转账
//                                    LogUtils.i("开始转账", nonce);
//                                    //普通转账
//                                    if (assetListBean == null) {
//                                        transactionManager.startEthTransaction(nonce, payAddress,
//                                                EnvironmentVariable.getProperty(Constants.CHAT_USER_ID),
//                                                receiveAddress, "",
//                                                Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger(),
//                                                credentials, EthTokenTransactionManager.ETH_COIN_CHAINID,
//                                                Convert.toWei(gas, Convert.Unit.ETHER).toBigInteger().divide(Contract.GAS_LIMIT),
//                                                Contract.GAS_LIMIT, new EthTokenTransactionManager.TransactionCallBack<String>() {
//                                                    @Override
//                                                    public void transactionResult(boolean isSuccess, String result) {
//                                                        if (!isSuccess) {
//                                                            onFailAnim();
//                                                            return;
//                                                        }
//                                                        //转账成功
//
//                                                        try {
//                                                            onSuccessView(result);
//                                                        } catch (Exception e) {
//                                                            e.printStackTrace();
//                                                        }
//
//                                                    }
//                                                });
//                                    } else {
//                                        //智能合约转账
//                                        transactionManager.startEthTokenTransaction(nonce, payAddress,
//                                                EnvironmentVariable.getProperty(Constants.CHAT_USER_ID),
//                                                receiveAddress, "",
//                                                Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger(),
//                                                credentials, Convert.toWei(gas, Convert.Unit.ETHER).toBigInteger().divide(Contract.GAS_LIMIT),
//                                                assetListBean.getAddress(),
//                                                Byte.valueOf(assetListBean.getBlockChainID()), assetListBean.getId(),
//                                                assetListBean.getName(), new EthTokenTransactionManager.TransactionCallBack<String>() {
//                                                    @Override
//                                                    public void transactionResult(boolean isSuccess, String result) {
//                                                        if (!isSuccess) {
//                                                            onFailAnim();
//                                                            return;
//                                                        }
//                                                        //转账成功
//                                                        // TODO: 18-6-14
//                                                        try {
//                                                            onSuccessView(result);
//                                                        } catch (Exception e) {
//                                                            e.printStackTrace();
//                                                        }
//
//                                                    }
//                                                });
//                                    }
//                                }
//                            });
//                        }
//                    });
//    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dispose();
        transactionManager.dispose();
    }

    private void initView() {
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ivTranferStatus = (ImageView) findViewById(R.id.transfer_anim);
//        Bitmap bitmap = ((BitmapDrawable) ivTranferStatus.getBackground()).getBitmap();
        animationDrawable = (AnimationDrawable) ivTranferStatus.getBackground();
        animationDrawable.start();
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(ResStringUtil.getString(R.string.open_planet_transfer_detail));
        tvCode = (TextView) findViewById(R.id.tv_transfer_code);
        tvCode.setText("");
        tvCodeIntro = (TextView) findViewById(R.id.tv_transaction_no);
        tvMyAddress = (TextView) findViewById(R.id.tv_sell_account);
        tvMyAddress.setText(payAddress);
        tvToAddress = (TextView) findViewById(R.id.tv_buy_account);
        tvToAddress.setText(receiveAddress);
        tvMyAddressIntro = (TextView) findViewById(R.id.tv_sell_account_intro);
        tvToAddressIntro = (TextView) findViewById(R.id.tv_buy_account_intro);
        tvCount = (TextView) findViewById(R.id.tv_count);
        tvCountUnit = (TextView) findViewById(R.id.tv_count_unit);
        tvTransStatus = (TextView) findViewById(R.id.tv_welcome);
        tvTransStatus.setText(ResStringUtil.getString(R.string.open_planet_transferring));

        tvTime = (TextView) findViewById(R.id.tv_transtime);
        tvTimeValue = (TextView) findViewById(R.id.tv_transtime_value);

    }

    //转账失败
    private void onFailAnim() {
        animationDrawable.stop();
        ivTranferStatus.setBackground(null);
        ivTranferStatus.setImageDrawable(ContextCompat.getDrawable(NewTransferSuccessActivity.this, R.drawable.transfer_fail));
        tvCodeIntro.setVisibility(View.GONE);
        tvCode.setText("");
        tvTransStatus.setVisibility(View.VISIBLE);
        tvTransStatus.setText(ResStringUtil.getString(R.string.open_planet_transaction_fail));
        tvMyAddress.setVisibility(View.VISIBLE);
        tvToAddress.setVisibility(View.VISIBLE);
        tvMyAddressIntro.setVisibility(View.VISIBLE);
        tvToAddressIntro.setVisibility(View.VISIBLE);
    }

    private void onSuccessView(String result) {
        JSONObject jsonObject = JSONObject.fromObject(result);
        animationDrawable.stop();
        ivTranferStatus.setBackground(null);
        ivTranferStatus.setImageDrawable(ContextCompat.getDrawable(NewTransferSuccessActivity.this, R.drawable.transfer_success));
        tvCount.setText("-" + amount);
        tvCount.setVisibility(View.VISIBLE);
        tvTransStatus.setText(ResStringUtil.getString(R.string.open_planet_transfer_success));
        tvCountUnit.setVisibility(View.VISIBLE);
        tvCodeIntro.setVisibility(View.VISIBLE);
        tvCode.setVisibility(View.VISIBLE);
        tvCode.setText(jsonObject.optString("transactionHash", ""));
        tvMyAddress.setVisibility(View.VISIBLE);
        tvToAddress.setVisibility(View.VISIBLE);
        tvMyAddressIntro.setVisibility(View.VISIBLE);
        tvToAddressIntro.setVisibility(View.VISIBLE);

        tvTime.setVisibility(View.VISIBLE);
        tvTimeValue.setVisibility(View.VISIBLE);

        try {

            Date date = sf1.parse(jsonObject.getString("time"));
            transferTime = sf.format(date);
            tvTimeValue.setText(transferTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
